//
//  NTChartView.m
//  chart
//
//  Created by lee jory on 09-10-15.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "NTChartView.h"

static int MARGIN_LEFT = 50;
static int MARGIN_BOTTOM = 30;
static int MARGIN_TOP = 20;
static int SHOW_SCALE_NUM = 5; 


@interface NTChartView(private)
-(void)drawColumn:(CGContextRef)context rect:(CGRect)_rect;
-(void)drawXAxis:(CGContextRef)context rect:(CGRect)_rect;
-(void)drawScale:(CGContextRef)context rect:(CGRect)_rect;
-(void)calcScales:(CGRect)_rect;
@end

@implementation NTChartView
@synthesize yData;
@synthesize xData;
@synthesize ySuffix;

- (void) dealloc
{
	[yData release];
	[super dealloc];
}

-(void)Refresh{
/*	
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
	CGContextFillRect(context, _rect);
	
	计算刻度
	[self calcScales:_rect];
	
	//画刻度
	[self drawScale:context rect:_rect];
	
	//画X轴文字
	[self drawXAxis:context rect:_rect];
	
	//画柱
	[self drawColumn:context rect:_rect];
 */
}


-(void)drawRect:(CGRect)_rect{
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	//CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
	
	CGContextSetFillColorWithColor(context,[UIColor colorWithRed:graphBackgroundRed green:graphBackgroundGreen blue:graphBackgroundBlue alpha:graphBackgroundAlpha].CGColor);
	//CGContextSetFillColorWithColor(context,[UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]].CGColor);;

	
	CGContextFillRect(context, _rect);
	
	//计算刻度
	[self calcScales:_rect];
	
	//画刻度
	[self drawScale:context rect:_rect];
	
	//画X轴文字
	[self drawXAxis:context rect:_rect];
	
	//画柱
	[self drawColumn:context rect:_rect];

}

-(void)drawScale:(CGContextRef)context rect:(CGRect)_rect{
	CGPoint points[3];
	points[0] = CGPointMake(MARGIN_LEFT - 10, MARGIN_TOP);
	points[1] = CGPointMake(MARGIN_LEFT -10, _rect.size.height - MARGIN_BOTTOM + 1);
	points[2] = CGPointMake(_rect.size.width - 10, _rect.size.height - MARGIN_BOTTOM + 1);
	CGContextSetAllowsAntialiasing(context, NO);
	CGContextAddLines(context, points, 3);
	
	
	CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
	
	for(int i=0;i<SHOW_SCALE_NUM + 1; i++){
		maxScaleHeight = (_rect.size.height - MARGIN_BOTTOM) * ( i ) / (SHOW_SCALE_NUM + 1);
		int vScal = ceil(1.0 * maxScaleValue / (SHOW_SCALE_NUM ) * (i ));
		float y = (_rect.size.height - MARGIN_BOTTOM) -
			maxScaleHeight;
		
		NSString *scaleStr = [NSString stringWithFormat:@"%d",vScal];
		[scaleStr
			drawAtPoint:CGPointMake(MARGIN_LEFT - 20 - [scaleStr sizeWithFont:[UIFont systemFontOfSize:12]].width, y - 10) withFont:[UIFont systemFontOfSize:12]];
		points[0] = CGPointMake(MARGIN_LEFT - 10, y);
		points[1] = CGPointMake(MARGIN_LEFT - 13, y);
		CGContextSetLineDash(context, 0, NULL, 0);
		CGContextAddLines(context, points, 2);
		CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

		CGContextDrawPath(context, kCGPathStroke);

		
		points[0] = CGPointMake(MARGIN_LEFT - 10, y);
		points[1] = CGPointMake(_rect.size.width - 10 , y);
		float partren[] = {2,3};
		CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:.7 green:.7 blue:.7 alpha:1].CGColor);

		CGContextSetLineDash(context, 0,partren , 2);
		CGContextAddLines(context, points, 2);
		CGContextDrawPath(context, kCGPathStroke);
		
	}
	
	CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

	CGContextDrawPath(context, kCGPathStroke);
	CGContextSetAllowsAntialiasing(context, YES);
}

-(void)drawColumn:(CGContextRef)context rect:(CGRect)_rect{
	int gNumber = 0, vNumber = 0;
	int baseGroundY = _rect.size.height - MARGIN_BOTTOM, baseGroundX = MARGIN_LEFT;
	CGPoint points[4];
	
	UIColor *columnColor = [UIColor colorWithRed:columnBackgroundRed green:columnBackgroundGreen 
											blue:columnBackgroundBlue alpha:columnBackgroundAlpha];
	
	CGContextSetFillColorWithColor(context, columnColor.CGColor);
	CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
	
	for(NSArray *g in yData){
		vNumber = 0;
		for(NSNumber *v in g){

			float columnHeight = [v floatValue] / maxScaleValue * maxScaleHeight ;
			
			//画正面
			CGContextSetFillColorWithColor(context, columnColor.CGColor);
			CGContextAddRect(context, CGRectMake(vNumber * 20 + baseGroundX + columnWidth * vNumber
												 , baseGroundY - columnHeight 
												 , columnWidth
												 , columnHeight));
			CGContextDrawPath(context, kCGPathFill);
			NSLog(@"columnHeight:%f, (_rect.size.height - MARGIN_TOP - MARGIN_BOTTOM ):%f",columnHeight,(_rect.size.height - MARGIN_TOP - MARGIN_BOTTOM ));

			NSLog(@"x point %f",vNumber * 20 + baseGroundX + columnWidth * vNumber);
			NSLog(@"y point %f",baseGroundY - columnHeight);
			
			//画顶部数字
			CGRect textRect = CGRectMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + 6 
										 , baseGroundY - columnHeight - 28
										 , columnWidth+10
										 , 10);
			UIFont* font = [UIFont systemFontOfSize:11];
			NSString *tmp = [NSString stringWithFormat:@"%d%@",[v intValue],self.ySuffix];
			[tmp drawInRect:textRect withFont:font];
			
			
			if(columnHeight < 10){
				vNumber++;
				continue;
			}
			
			//画右侧面
			CGContextSetFillColorWithColor(context, columnColor.CGColor);
			points[0] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth, baseGroundY - columnHeight -10);
			points[1] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth + sideWidth, baseGroundY - columnHeight -10 );
			points[2] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth + sideWidth, baseGroundY );
			points[3] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth, baseGroundY );
			
			CGContextAddLines(context, points, 4);
			CGContextDrawPath(context, kCGPathFill);
			
			//画上面
			CGContextSetFillColorWithColor(context, columnColor.CGColor);
			points[0] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber , baseGroundY - columnHeight );
			points[1] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + sideWidth, baseGroundY - columnHeight -10 );
			points[2] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth + sideWidth , baseGroundY - columnHeight -10 );
			points[3] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth, baseGroundY - columnHeight );
			
			CGContextAddLines(context, points, 4);
			CGContextDrawPath(context, kCGPathFill);
			
			vNumber++;
		}
		gNumber ++; 
	}
}

//画X轴文字
-(void)drawXAxis:(CGContextRef)context rect:(CGRect)_rect{
	int vNumber = 0;
	int baseGroundY = _rect.size.height - MARGIN_BOTTOM, baseGroundX = MARGIN_LEFT;
	
	for(NSString *str in xData){
		//画底部部数字
		CGRect textRect = CGRectMake(vNumber * 20 + baseGroundX + columnWidth * vNumber
										 , baseGroundY+2
										 , columnWidth
										 , 10);
		UIFont* font = [UIFont systemFontOfSize:10];
		[str drawInRect:textRect withFont:font];
		vNumber ++;
	}		
}

-(void)calcScales:(CGRect)_rect{
	int columnCount = 0;
	for(NSArray *g in yData){
		for(NSNumber *v in g){
			if(maxValue<[v floatValue]) maxValue = [v floatValue];
			if(minValue>[v floatValue]) minValue = [v floatValue];
			columnCount++;
		}
	}
	
	maxScaleValue = ((int)ceil(maxValue) + (SHOW_SCALE_NUM - (int)ceil(maxValue) % SHOW_SCALE_NUM));
	
	columnWidth = (_rect.size.width - MARGIN_LEFT * 2) / (columnCount + 1);
	sideWidth = columnWidth *.2;
	columnWidth *= .8;	
}
@end
